//
//  BigImageViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/11.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "BigImageViewController.h"
#import "UIView+Layout.h"
#import "TZTestCell.h"
#import "CoreDataHandle.h"
#import "Photos.h"
@interface BigImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)  UICollectionView * collectionView;
@property(nonatomic,strong) NSMutableArray *photoArr;
@property(nonatomic,assign) CGFloat itemWH;
@property(nonatomic,assign)  CGFloat margin;

@end

@implementation BigImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.view.backgroundColor = [UIColor blackColor];
    [self arrayGetData];
    [self configCollectionView];
    
    // Do any additional setup after loading the view.
    
}
- (void)configCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //(设置行间距(每个item上下距离)
    layout.minimumLineSpacing = 20;
    //(设置左右间距(列间距
    layout.minimumInteritemSpacing =20;
    //设置距屏幕边缘的距离
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width-20, [UIScreen mainScreen].bounds.size.height-20);
   layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
//    layout.
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100,[UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height-180) collectionViewLayout:layout];

    _collectionView.backgroundColor = [UIColor blackColor];
    _collectionView.pagingEnabled =YES;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width*self.indexpath, 0);
    
    
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"%lu",(unsigned long)self.photoArr.count);
    return self.photoArr.count ;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TZTestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZTestCell" forIndexPath:indexPath];
          cell.imageView.image = self.photoArr[indexPath.row];
    
    
    return cell;
}

-(void)arrayGetData{
    [[CoreDataHandle shareCoreDataHandle] getCoreData];
    
    //创建请求对象,并且指明所有数据的类型
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Photos"];
    NSArray *objectArray =    [[CoreDataHandle shareCoreDataHandle].context executeFetchRequest:request error:nil];
    NSMutableArray *arr = [NSMutableArray array];
    if (objectArray) {
        for (Photos *object in objectArray) {
            NSString * conStr = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:object.imageStr];
            UIImage *image = [UIImage imageWithContentsOfFile:conStr];
            [arr addObject:image];
       
        }
        self.photoArr  = [NSMutableArray arrayWithArray:arr];
   
            }
}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
      self.title = [NSString stringWithFormat:@"%ld/%lu",indexPath.row+1,(unsigned long)self.photoArr.count];
    
}

@end
